﻿@model BuyViewModel

@{
    ViewData["Title"] = "购买商品";
}

<h2>@ViewData["Title"]</h2>

<div class="row">
    <div class="col-md-6">
        <form asp-action="Buy">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <input type="hidden" asp-for="ProductId" />

            <div class="form-group mb-3">
                <label class="form-label">产品编号</label>
                <input class="form-control" value="@Model.ProductNo" readonly>
            </div>

            <div class="form-group mb-3">
                <label class="form-label">产品名称</label>
                <input class="form-control" value="@Model.ProductName" readonly>
            </div>

            <div class="form-group mb-3">
                <label class="form-label">单价</label>
                <input class="form-control" value="@Model.Price.ToString("C")" readonly>
            </div>

            <div class="form-group mb-3">
                <label asp-for="Quantity" class="form-label"></label>
                <input asp-for="Quantity" class="form-control"
                       min="1" max="@Model.Stock">
                <span asp-validation-for="Quantity" class="text-danger"></span>
            </div>

            <div class="form-group mb-3">
                <label class="form-label">总价</label>
                <input id="totalAmount" class="form-control"
                       value="@Model.TotalAmount.ToString("C")" readonly>
            </div>

            <div class="form-group mb-3">
                <label asp-for="DeliveryTime" class="form-label"></label>
                <input asp-for="DeliveryTime" type="datetime-local"
                       class="form-control"
                       min="@DateTime.Now.ToString("yyyy-MM-ddTHH:mm")">
                <span asp-validation-for="DeliveryTime" class="text-danger"></span>
            </div>

            <button type="submit" class="btn btn-primary">提交订单</button>
        </form>
    </div>
</div>

@section Scripts {
    @{
        await Html.RenderPartialAsync("_ValidationScriptsPartial");
    }
    <script>
        document.getElementById('Quantity').addEventListener('input', function() {
            const quantity = parseInt(this.value) || 0;
            const price = @Model.Price;
            document.getElementById('totalAmount').value =
                (quantity * price).toLocaleString('zh-CN', {
                    style: 'currency',
                    currency: 'CNY'
                });
        });
    </script>
}